package elephant.rpc.server.core;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * 
 * @author skydu
 *
 */
@SuppressWarnings("serial")
public class PerformMonitor implements Serializable{
	
	public LongAdder trafficIn=new LongAdder();
	public LongAdder trafficOut=new LongAdder();
	public LongAdder trafficInCount=new LongAdder();
	public LongAdder trafficOutCount=new LongAdder();
	public AtomicLong maxInvokeTime = new AtomicLong(0);
	public AtomicLong minInvokeTime = new AtomicLong(Integer.MAX_VALUE);
	public AtomicLong lastInvokeTime = new AtomicLong();
	public AtomicLong totalInvokeTime = new AtomicLong();
	public AtomicLong invokeCount = new AtomicLong();
	public AtomicLong exceptionCount = new AtomicLong();
	//
	public PerformMonitor(){
		
	}
	//
	public void addTrafficIn(int dataLength){
		trafficIn.add(dataLength);
		trafficInCount.increment();
	}
	//
	public void addTrafficOut(int dataLength){
		trafficOut.add(dataLength);
		trafficOutCount.increment();
	}
	//
	public void invokeMethod(long time, boolean exception) {
		lastInvokeTime.set(time);
		totalInvokeTime.addAndGet(time);
		//reset counter.
		if(totalInvokeTime.longValue()<0){
			totalInvokeTime.set(0);
		}
		if(invokeCount.longValue()<0){
			invokeCount.set(0L);
		}
		invokeCount.incrementAndGet();
		if (minInvokeTime.longValue() > time) {
			minInvokeTime.set(time);
		}
		if (maxInvokeTime.longValue() < time) {
			maxInvokeTime.set(time);
		}
		if (exception) {
			if(exceptionCount.longValue()<0){
				exceptionCount.set(0L);
			}
			exceptionCount.incrementAndGet();
		}
	}
}
